package org.example.mail;

import cn.hutool.extra.mail.MailAccount;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author weigang
 * @create 2022-10-27 17:38
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(HutoolMailConfigurer.HutoolMailConfig.class)
public class HutoolMailConfigurer {
    @Bean
    public MailAccount bloomHutoolMailAccount(HutoolMailConfig hutoolMailConfig) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setFrom(hutoolMailConfig.getFrom());
        mailAccount.setHost(hutoolMailConfig.getHost());
        mailAccount.setTimeout(hutoolMailConfig.getTimeout());
        mailAccount.setConnectionTimeout(hutoolMailConfig.getConnectionTimeout());
        mailAccount.setUser(hutoolMailConfig.getUser());
        mailAccount.setPass(hutoolMailConfig.getPass());
        mailAccount.setPort(hutoolMailConfig.getPort());
        mailAccount.setStarttlsEnable(hutoolMailConfig.getStartttlsEnable());

        log.info("wgHutoolMailAccount:{}", mailAccount);
        return mailAccount;
    }

    @Data
    @ConfigurationProperties(prefix = "wg.hutool.mail")
    public static class HutoolMailConfig {
        private String from;
        private String host;
        private Long timeout;
        private Long connectionTimeout;
        private String user;
        private String pass;
        private Integer port;
        private Boolean startttlsEnable;
    }
}
